package com.ailanyin.modules.flowable.controller;

import cn.hutool.json.JSONUtil;
import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.database.result.CommonPage;
import com.ailanyin.common.security.utils.SecurityUtil;
import com.ailanyin.modules.flowable.domain.bo.WfCopyBo;
import com.ailanyin.modules.flowable.domain.bo.WfProcessBo;
import com.ailanyin.modules.flowable.domain.vo.WfCopyVo;
import com.ailanyin.modules.flowable.domain.vo.WfDefinitionVo;
import com.ailanyin.modules.flowable.domain.vo.WfTaskVo;
import com.ailanyin.modules.flowable.service.IWfCopyService;
import com.ailanyin.modules.flowable.service.IWfProcessService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 工作流流程管理
 *
 * @author KonBAI
 * @createTime 2022/3/24 18:54
 */
@Slf4j
@RestController
@RequestMapping("/workflow/process")
public class WfProcessController {

    @Autowired
    private IWfProcessService processService;
    @Autowired
    private IWfCopyService copyService;

    @GetMapping(value = "/list")
    public CommonPage<WfDefinitionVo> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return processService.processList(pageNum, pageSize);
    }

    /**
     * 查询流程部署关联表单信息
     *
     * @param definitionId 流程定义id
     * @param deployId     流程部署id
     */
    @GetMapping("/getProcessForm")
    public AjaxResult getForm(@RequestParam(value = "definitionId") String definitionId,
                              @RequestParam(value = "deployId") String deployId) {
        return AjaxResult.success(processService.selectFormContent(definitionId, deployId));
    }

    /**
     * 根据流程定义id启动流程实例
     *
     * @param processDefId 流程定义id
     * @param variables    变量集合,json对象
     */
    @PostMapping("/start")
    public AjaxResult start(@RequestParam(value = "processDefId") String processDefId, @RequestBody Map<String, Object> variables) {
        processService.startProcess(processDefId, variables);
        return AjaxResult.success("流程启动成功");

    }

    /**
     * 读取xml文件
     *
     * @param processDefId 流程定义ID
     */
    @GetMapping("/bpmnXml/{processDefId}")
    public AjaxResult getBpmnXml(@PathVariable(value = "processDefId") String processDefId) {
        return AjaxResult.success(null, processService.queryBpmnXmlById(processDefId));
    }

    /**
     * 查询流程详情信息
     *
     * @param procInsId 流程实例ID
     * @param deployId  部署ID
     * @param taskId    任务ID
     */
    @GetMapping("/detail")
    public AjaxResult detail(String procInsId, String deployId, String taskId) {
        return AjaxResult.success(JSONUtil.toJsonStr(processService.queryProcessDetail(procInsId, deployId, taskId)));
    }

    @GetMapping(value = "/ownList")
    public CommonPage<WfTaskVo> ownProcess(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return processService.queryPageOwnProcessList(pageNum, pageSize);
    }


    @GetMapping(value = "/todoList")
    public CommonPage<WfTaskVo> todoProcess(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return processService.queryPageTodoProcessList(pageNum, pageSize);
    }


    @GetMapping(value = "/claimList")
    public CommonPage<WfTaskVo> claimProcess(WfProcessBo processBo,
                                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return processService.queryPageClaimProcessList(processBo, pageNum, pageSize);
    }

    @GetMapping(value = "/finishedList")
    public CommonPage<WfTaskVo> finishedProcess(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return processService.queryPageFinishedProcessList(pageNum, pageSize);
    }


    @GetMapping(value = "/copyList")
    public CommonPage<WfCopyVo> copyProcess(WfCopyBo copyBo,
                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        copyBo.setUserId(SecurityUtil.getLoginUserId());
        PageHelper.startPage(pageNum, pageSize);
        return CommonPage.restPage(copyService.queryList(copyBo));
    }
}
